package com.wr.listener;

import com.wr.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author WuRui
 * @Date 2017/5/22 0022
 */
@WebListener
public class SessionListener implements HttpSessionListener {

    @Autowired
    private SessionService sessionService;

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        String mobile = (String) session.getAttribute("mobile");
        if(mobile != null && !"".equals(mobile)){
            sessionService.deleteSessionByMobile(mobile);
        }
    }
}
